package com.weds.edpf.core.service;

import com.weds.bean.jwt.JwtUtils;
import com.weds.core.resp.JsonResult;
import com.weds.core.utils.StringUtils;
import com.weds.edpf.core.entity.AcUserVO;
import com.weds.edpf.core.entity.NTtCardEntity;
import com.weds.edpf.core.entity.UserCardVO;
import com.weds.edpf.core.entity.WtPublicEntity;
import com.weds.edpf.core.mapper.WtPublicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Date;
import java.util.List;

import com.weds.core.base.BaseService;
import com.weds.edpf.core.mapper.NDtCardMapper;
import com.weds.edpf.core.entity.NDtCardEntity;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author wangjianqiang
 * @Description 卡管理
 * @Date 2020-06-16
 * @Version 新增: 发卡，已发卡列表，未发卡列表接口  wangjianqiang  2020/06/17
 * 新增: 挂失，解挂接口  wangjianqiang  2020/06/18
 */
@Service
public class NDtCardService extends BaseService {

    @Autowired
    private NDtCardMapper nDtCardMapper;

    @Autowired
    private WtPublicMapper wtPublicMapper;

    @Autowired
    UserBaseService userBaseService;

    private Logger log = LogManager.getLogger();

    public int deleteByPrimaryKey(String cardSerial) {
        return nDtCardMapper.deleteByPrimaryKey(cardSerial);
    }

    public int insert(NDtCardEntity record) {
        return nDtCardMapper.insert(record);
    }

    public int insertSelective(NDtCardEntity record) {
        return nDtCardMapper.insertSelective(record);
    }

    public NDtCardEntity selectByPrimaryKey(String cardSerial) {
        return nDtCardMapper.selectByPrimaryKey(cardSerial);
    }

    public int updateByPrimaryKeySelective(NDtCardEntity record) {
        return nDtCardMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(NDtCardEntity record) {
        return nDtCardMapper.updateByPrimaryKey(record);
    }

    public List<NDtCardEntity> selectListByEntity(NDtCardEntity record) {
        return nDtCardMapper.selectListByEntity(record);
    }


    public int deleteBatchByKeys(List<NDtCardEntity> list) {
        return nDtCardMapper.deleteBatchByKeys(list);
    }

    public int insertBatch(List<NDtCardEntity> list) {
        return nDtCardMapper.insertBatch(list);
    }


    public NDtCardEntity selectByCardNo(String cardHao) {
        return nDtCardMapper.selectByCardNo(cardHao);
    }

    public int countCardType(Long userSerial, Integer cardType) {
        return nDtCardMapper.countCardType(userSerial, cardType);
    }

    public int preCardAdd(NDtCardEntity record) {
        return nDtCardMapper.preCardAdd(record);
    }


    public int cardUnLoss(NDtCardEntity record) {
        return nDtCardMapper.cardUnLoss(record);
    }

    public int cardBack(NDtCardEntity record) {
        return nDtCardMapper.cardBack(record);
    }

    public NDtCardEntity selectByCardNoSerial(String cardHao, Long userSerial) {
        return nDtCardMapper.selectByCardNoSerial(cardHao, userSerial);
    }

    public String selectCardSerial(Long user_serial, Integer cardType) {
        return nDtCardMapper.selectCardSerial(user_serial, cardType);
    }


    public int updateCardKData(NDtCardEntity record) {
        return nDtCardMapper.updateCardKData(record);
    }


    public List<UserCardVO> cardIssueList(UserCardVO userCardVO) {
        return nDtCardMapper.cardIssueList(userCardVO);
    }

    public List<UserCardVO> cardNotIssueList(UserCardVO userCardVO) {
        return nDtCardMapper.cardNotIssueList(userCardVO);
    }


    public AcUserVO selectAcUserInfo(Long userSerial) {
        return nDtCardMapper.selectAcUserInfo(userSerial);
    }

    public AcUserVO selectAcInfo(String userSerial) {
        return nDtCardMapper.selectAcInfo(userSerial);
    }

    public Integer selectCardLx(String cardSerial) {
        return nDtCardMapper.selectCardLx(cardSerial);
    }

    public AcUserVO selectUserInfo(Long userSerial) {
        return nDtCardMapper.selectUserInfo(userSerial);
    }

    /**
     * 挂失
     *
     * @param record
     * @return
     */
    @Transactional
    public JsonResult<Object> cardloss(NDtCardEntity record) {
        //挂失卡过程
        int i = nDtCardMapper.cardloss(record);
        if (i == 2) {
            return failMsg("人员不存在");
        }
        if (i == 3) {
            return failMsg("卡号状态不正常 无法挂失");
        }
        if (i != 1) {
            return failMsg("挂失处理过程失败");
        }
        String cardSerival = record.getCardSerial();
        //获取卡序号
        if (StringUtils.isBlank(cardSerival)) {
            NDtCardEntity entity = nDtCardMapper.selectByCardNoSerial(record.getCardHao(), record.getUserSerial());
            cardSerival = entity.getCardSerial();
        }

        WtPublicEntity wtPublicEntity = new WtPublicEntity();
        wtPublicEntity.setLx(8);
        wtPublicEntity.setLogType(6);
        wtPublicEntity.setIsAll(0);
        wtPublicEntity.setUserSerial(record.getUserSerial());
        wtPublicEntity.setCardSerial(Long.parseLong(cardSerival));
        wtPublicEntity.setLogSj(new Date());
        wtPublicEntity.setLogIp(record.getIp());
        wtPublicEntity.setGlyNo(record.getGlyNo());
        // 增量下发
        wtPublicMapper.insertSelective(wtPublicEntity);

        return succMsg("操作成功");
    }

    /**
     * 解挂
     * @param record
     * @return
     */
    @Transactional
    public JsonResult<Object> cardUnloss(NDtCardEntity record) {

        String cardSerival = record.getCardSerial();
        //获取卡序号
        if (StringUtils.isBlank(cardSerival)) {
            NDtCardEntity entity = nDtCardMapper.selectByCardNoSerial(record.getCardHao(), record.getUserSerial());
            record.setCardSerial(entity.getCardSerial());
        }
        //解挂
        int i = nDtCardMapper.cardUnLoss(record);
        if(i == 2){
            return failMsg("不存在挂失卡片,无法解挂");
        }
        if(i != 1){
            return failMsg("解挂失败");
        }
        // 增量下发
        WtPublicEntity wtPublicEntity = new WtPublicEntity();
        wtPublicEntity.setLx(8);
        wtPublicEntity.setLogType(7);
        wtPublicEntity.setIsAll(0);
        wtPublicEntity.setUserSerial(record.getUserSerial());
        wtPublicEntity.setCardSerial(Long.parseLong(record.getCardSerial()));
        wtPublicEntity.setLogSj(new Date());
        wtPublicEntity.setLogIp(record.getIp());
        wtPublicEntity.setGlyNo(record.getGlyNo());
        wtPublicMapper.insertSelective(wtPublicEntity);

        return succMsg("操作成功");
    }

    /**
     * 发卡
     * @param record
     * @return
     */
    @Transactional
    public JsonResult<Object> cardIssue(NDtCardEntity record){

        //调用预发卡存储过程
        NDtCardEntity entity = new NDtCardEntity();
        entity.setLx(0);
        entity.setUserSerial(record.getUserSerial());
        entity.setCardHao(record.getCardHao());
        entity.setCardSerial(record.getCardSerial());
        entity.setCardLx(record.getCardLx());
        entity.setCardWork(record.getCardWork());
        entity.setCardType(record.getCardType());
        entity.setGlyNo(record.getGlyNo());
        entity.setDevSerial("");
        entity.setAcType("");
        entity.setJmKh("");
        entity.setIp(record.getIp());
        int i = nDtCardMapper.preCardAdd(entity);
        if (i == 2) {
            return failMsg("人员已有卡");
        }
        if (i == 3) {
            return failMsg("新卡被占用");
        }
        if (i != 1) {
            return failMsg("预发卡执行失败");
        }
        //判断是否存在坏卡
        int badCard = nDtCardMapper.countCardType(record.getUserSerial(), 5);
        if (badCard != 0) {
            //存在坏卡
            NDtCardEntity entity1 = nDtCardMapper.selectByCardNoSerial(record.getCardHao(), record.getUserSerial());
            if (entity1.getLx() == null) {
                return failMsg("当前的卡片与之前未完成的卡不是同一张,请换卡重试");
            }
            if (!entity1.getLx().equals(record.getLx())) {
                return failMsg("当前选择的卡类型与未完成时的卡类型不一致,请重新选择");
            }
            String cardServial = nDtCardMapper.selectCardSerial(record.getUserSerial(), 148);
            if (StringUtils.isBlank(cardServial)) {
                return failMsg("当前的卡片与之前未完成的卡不是同一张,请换卡重试");
            }
        }
        //更新控卡密钥
        NDtCardEntity entity2 = new NDtCardEntity();
        entity2.setUserSerial(record.getUserSerial());
        entity2.setCardHao(record.getCardHao());
        entity2.setCardKdata("");
        nDtCardMapper.updateCardKData(entity2);


        //调用正式发卡存储过程
        entity.setLx(1);
        entity.setCardType(0);
        i = nDtCardMapper.preCardAdd(entity);
        if (i == 2) {
            return failMsg("人员已有卡");
        }
        if (i == 3) {
            return failMsg("新卡被占用");
        }
        if (i != 1) {
            return failMsg("发卡执行失败");
        }
        //下发增量
        WtPublicEntity wtPublicEntity = new WtPublicEntity();
        wtPublicEntity.setLx(8);
        wtPublicEntity.setLogType(4);
        wtPublicEntity.setIsAll(0);
        wtPublicEntity.setUserSerial(record.getUserSerial());
        wtPublicEntity.setCardSerial(Long.parseLong(record.getCardSerial()));
        wtPublicEntity.setLogSj(new Date());
        wtPublicEntity.setLogIp(record.getIp());
        wtPublicEntity.setGlyNo(record.getGlyNo());
        wtPublicMapper.insertSelective(wtPublicEntity);

        Integer photo = userBaseService.selectUserPhoto(record.getUserSerial());
        if (photo != null && photo == 1) {
            //插入新增档案照片数量

        }
        return succMsg();
    }




    @Transactional
    public JsonResult<Object> cardBacks(NDtCardEntity record){

        //预退卡过程
        int i = nDtCardMapper.cardBack(record);
        if (i != 1) {
            return failMsg("预退卡过程失败");
        }
        // todo 判断人员是否有档案照片,用于给设备下发档案

        /*// 查询个人信息
        AcUserVO acUserVO = nDtCardService.selectUserInfo(record.getUserSerial());
        // 查询账户信息
        AcUserVO acInfo = nDtCardService.selectAcInfo(record.getUserSerial()+"");*/

        //退卡
        record.setCardType(2);
        record.setLx(1);
        i = nDtCardMapper.cardBack(record);
        if (i != 1) {
            return failMsg("退卡过程失败");
        }

        WtPublicEntity wtPublicEntity = new WtPublicEntity();
        wtPublicEntity.setLx(8);
        wtPublicEntity.setLogType(8);
        wtPublicEntity.setIsAll(0);
        wtPublicEntity.setUserSerial(record.getUserSerial());
        wtPublicEntity.setCardSerial(Long.parseLong(record.getCardSerial()));
        wtPublicEntity.setLogSj(new Date());
        wtPublicEntity.setLogIp(record.getIp());
        wtPublicEntity.setGlyNo(record.getGlyNo());
        // 增量下发
        wtPublicMapper.insertSelective(wtPublicEntity);

        return succMsg("操作成功");

    }


}
